package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.pojo.Label;
import com.woniu.pojo.TeamLabel;
import com.woniu.mapper.TeamLabelMapper;
import com.woniu.service.ITeamLabelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 团队——标签中间表 服务实现类
 * </p>
 *
 * @author xiaotang
 * @since 2022-10-12
 */
@Service
public class TeamLabelServiceImpl extends ServiceImpl<TeamLabelMapper, TeamLabel> implements ITeamLabelService {

    @Autowired
    private TeamLabelMapper teamLabelMapper;

    /**
     * 根据团队id查询标签信息
     * @param id
     * @return
     */
    @Override
    public List<Label> getByTeamId(Long id) {
        List<Label> labelList = teamLabelMapper.getByTeamId(id);
        return labelList;
    }

    /**
     * 对团队中间表进行编辑操作
     * @param teamId
     * @param selectedLabelIds
     */
    @Override
    public void addLabels(Long teamId, List<Long> selectedLabelIds) {
        QueryWrapper<TeamLabel> wrapper = new QueryWrapper<>();
        wrapper.eq("team_id", teamId);
        teamLabelMapper.delete(wrapper);
        log.debug("=======" + selectedLabelIds);
        // 将新的关联视频数据添加到表中
        if(selectedLabelIds != null && selectedLabelIds.size() > 0){
            for (Long selectedLabelId : selectedLabelIds) {
                TeamLabel tl = new TeamLabel();
                tl.setTeamId(teamId);
                tl.setLabelId(selectedLabelId);
                teamLabelMapper.insert(tl);
            }
        }
    }
}
